<template>
	<div class="content">
		<div class="content-header">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>系统管理</el-breadcrumb-item>
				<el-breadcrumb-item>
					<b>角色管理</b>
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="content-body">
			<el-scrollbar>
				<div class="panel">
					<el-form :model="sysRoleForm" ref="sysRoleForm" inline label-width="100px">
						<el-row>
							<el-col :md="8">
								<el-form-item label="角色名称">
									<el-input v-model="sysRoleForm.roleName" maxlength="50"></el-input>
								</el-form-item>
							</el-col>
							<el-col :md="16" class="search-btn">
								<div class="search-btn-right">
									<el-button type="success" icon="el-icon-plus" @click="handleAdd">新增</el-button>
									<el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
									<el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
								</div>
							</el-col>
						</el-row>
					</el-form>
					<el-table :data="tableData" border stripe style="width: 100%">
						<el-table-column label="序号" width="60" header-align="center" align="center">
							<template
								slot-scope="scope">{{scope.$index + 1 + (tableCurrentPage-1)*tablePageSize}}</template>
						</el-table-column>
						<el-table-column prop="roleCode" label="角色编码"></el-table-column>
						<el-table-column prop="roleName" label="角色名称"></el-table-column>
						<el-table-column prop="description" label="描述"></el-table-column>
						<el-table-column label="操作" min-width="150px">
							<template slot-scope="scope">
								<el-button type="warning" @click="handleUpdate(scope.row.id)">编辑</el-button>
								<el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page.sync="tableCurrentPage" :page-sizes="[10, 20, 30, 50]"
						:page-size.sync="tablePageSize" layout="total, jumper, prev, pager, next, sizes"
						:total="tableTotal"></el-pagination>
				</div>
			</el-scrollbar>
			<SysRole ref="sysRole"></SysRole>
		</div>
	</div>
</template>

<script>
	import SysRole from "./SysRole";
	export default {
		components: {
			SysRole
		},
		data() {
			return {
				sysRoleForm: {
					roleName: ""
				},
				tableTotal: 0,
				tableCurrentPage: 1,
				tablePageSize: 10,
				tableData: []
			};
		},
		mounted() {
			this.getTableData(this.tableCurrentPage, this.tablePageSize);
		},
		methods: {
			handleSizeChange(val) {
				this.getTableData(this.tableCurrentPage, val);
			},
			handleCurrentChange(val) {
				this.getTableData(val, this.tablePageSize);
			},
			handleSearch() {
				this.getTableData(this.tableCurrentPage, this.tablePageSize);
			},
			getTableData(page, pageSize) {

				let pageParams = '?page=' + page + '&rows=' + pageSize;

				this.$api.post('/role/findByPage' + pageParams, this.sysRoleForm).then(
					response => {
						this.tableData = response.data.list;
						this.tableTotal = response.data.total;
					}
				);
			},
			resetForm() {
				this.sysRoleForm.roleName = "";
				this.getTableData(1, this.tablePageSize);
			},
			handleAdd() {
				this.$refs.sysRole.initForm();
			},
			handleUpdate(id) {
				this.$refs.sysRole.initForm(id);
			},
			handleDelete(id) {
				this.$confirm("确认删除?", "提示", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						this.$api.post('/role/delete', {id:id}).then(response => {
							this.$message.success(response.message);
							this.handleSearch();
						});
					})
					.catch(() => {});
			}
		}
	};
</script>
